/*
 * Copyright (c) 2013, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.value;

import java.lang.reflect.Array;

import org.omorphdb.core.exception.CorruptedStorageException;
import org.omorphdb.core.io.Constants;
import org.omorphdb.core.storage.document.Document;

public class ArrayValue implements Value<Object>
{
	private final Value<?>[] value;
	private int size;

	public ArrayValue(Object array, ValueType componentType)
	{
		this.value = createInnerArray(array, componentType);

		calculateSize();
	}

	public ArrayValue(Value<?>[] array)
	{
		this.value = array;

		calculateSize();
	}

	private void calculateSize()
	{
		/* For the type */
		this.size = Constants.SIZEOF_BYTE + Constants.SIZEOF_INT;

		for (int i = 0; i < this.value.length; i++)
			this.size += this.value[i].size();
	}

	private Value<?>[] createInnerArray(Object obj, ValueType componentType)
	{
		int l = Array.getLength(obj);
		Value<?>[] array = new Value<?>[l];

		for (int i = 0; i < l; i++)
		{
			Object elem = Array.get(obj, i);

			if (elem.getClass().isArray()) array[i] = new ArrayValue(elem, componentType);
			else array[i] = convertElement(elem, componentType);
		}

		return array;
	}

	private Value<?> convertElement(Object elem, ValueType componentType)
	{
		switch (componentType)
		{
		case BOOLEAN:
			return ValueFactory.createBoolean((Boolean) elem);

		case BYTE:
			return ValueFactory.createByte((Byte) elem);

		case CHAR:
			return ValueFactory.createChar((Character) elem);

		case SHORT:
			return ValueFactory.createShort((Short) elem);

		case INT:
			return ValueFactory.createInteger((Integer) elem);

		case LONG:
			return ValueFactory.createLong((Long) elem);

		case FLOAT:
			return ValueFactory.createFloat((Float) elem);

		case DOUBLE:
			return ValueFactory.createDouble((Double) elem);

		case STRING:
			return ValueFactory.createString((String) elem);

		case DATE:
			return ValueFactory.createDate((Long) elem);

		case DOCUMENT:
			return new ObjectValue((Document) elem);

		default:
			throw new CorruptedStorageException("Invalid format");
		}
	}

	@Override
	public ValueType getType()
	{
		return ValueType.ARRAY;
	}

	@Override
	public Class<?> getJavaType()
	{
		if (this.value.length > 0)
		{
			Class<?> javaType = this.value[0].getJavaType();
			return Array.newInstance(javaType, 0).getClass();
		}

		return Object[].class;
	}

	/**
	 * This should returns a value of some array type, already converted to a
	 * valid java array.
	 */
	@Override
	public Object get()
	{
		int l = this.value.length;
		Class<?> componentType = l > 0 ? this.value[0].getJavaType() : Object[].class;
		Object converted = Array.newInstance(componentType, l);

		for (int i = 0; i < l; i++)
			Array.set(converted, i, this.value[i].get());

		return converted;
	}

	public int getLength()
	{
		return this.value.length;
	}

	public Value<?> get(int i)
	{
		return this.value[i];
	}

	@Override
	public int size()
	{
		return this.size;
	}

	@Override
	public boolean equals(Object value)
	{
		if (value instanceof ArrayValue)
		{
			Value<?>[] v = ((ArrayValue) value).value;

			for (int i = 0; i < v.length; i++)
				if (!v[i].equals(this.value[i])) return false;

			return true;
		}

		return false;
	}

	@Override
	public boolean isNull()
	{
		return this.value == null;
	}

	@Override
	public ArrayValue clone()
	{
		// FIXME A proper implementation is required.
		return this;
	}
}
